/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.l2j.gameserver.handler.bypasshandlers;

import net.sf.l2j.gameserver.handler.IBypassHandler;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;

/**
 * @author Anarchy
 *
 */
public class ClanNoticeBypasses implements IBypassHandler
{
	private static final String[] BYPASSES = { "bp_setNotice", "bp_removeNotice" };
	
	@Override
	public boolean handleBypass(String bypass, L2PcInstance activeChar)
	{
		if (bypass.startsWith("bp_setNotice"))
		{
			if (activeChar.getClan() == null || activeChar.getClan().getLeaderName() != activeChar.getName())
			{
				activeChar.sendMessage("You cannot do that.");
				return false;
			}
			
			String nt = bypass.substring(13);
			
			activeChar.getClan().setClanNotice(nt);
			activeChar.getClan().updateClanInDB();
			activeChar.sendMessage("Your clan's notice has been changed.");
			
			NpcHtmlMessage htm = new NpcHtmlMessage(0);
			htm.setFile("./data/html/custom/clannotice/main.htm");
			
			htm.replace("%clannotice%", activeChar.getClan().getClanNotice());
			
			activeChar.sendPacket(htm);
			
			NpcHtmlMessage htm2 = new NpcHtmlMessage(0);
			
			htm2.setFile("./data/html/custom/clannotice/notice.htm");
			
			htm2.replace("%clannotice%", activeChar.getClan().getClanNotice());
			
			for (L2PcInstance p : activeChar.getClan().getOnlineMembers(0))
			{
				if (p != activeChar)
				{
					p.sendPacket(htm2);
					p.sendMessage("You have a new clan notice.");
				}
			}
		}
		
		if (bypass.equals("bp_removeNotice"))
		{
			if (activeChar.getClan() == null || activeChar.getClan().getLeaderName() != activeChar.getName())
			{
				activeChar.sendMessage("You cannot do that.");
				return false;
			}
			
			if (activeChar.getClan().getClanNotice() == null)
			{
				NpcHtmlMessage htm = new NpcHtmlMessage(0);
				htm.setFile("./data/html/custom/clannotice/main.htm");
				
				htm.replace("%clannotice%", "You have not a clan notice set right now.");
				
				activeChar.sendPacket(htm);
				
				activeChar.sendMessage("You don't have a clan notice.");
				return false;
			}
			
			activeChar.getClan().setClanNotice(null);
			activeChar.getClan().updateClanInDB();
			activeChar.sendMessage("You have removed your clan notice.");
			
			NpcHtmlMessage htm = new NpcHtmlMessage(0);
			htm.setFile("./data/html/custom/clannotice/main.htm");
			
			htm.replace("%clannotice%", "You have not a clan notice set right now.");
			
			activeChar.sendPacket(htm);
		}
		
		return true;
	}
	
	@Override
	public String[] getBypassHandlersList()
	{
		return BYPASSES;
	}
}
